package com.wolfsea.viewpager2demo.ui.home
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.wolfsea.viewpager2demo.databinding.FragmentHomeBinding
import com.wolfsea.viewpager2demo.ui.adapter.ViewPage2Adapter
import com.wolfsea.viewpager2demo.ui.define.DIYPageTransformer

class HomeFragment : Fragment() {

    private var _binding: FragmentHomeBinding? = null

    private val binding get() = _binding!!

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {

        val homeViewModel =
            ViewModelProvider(this).get(HomeViewModel::class.java)

        _binding = FragmentHomeBinding.inflate(inflater, container, false)
        val root: View = binding.root
        //val textView: TextView = binding.textHome
        //homeViewModel.text.observe(viewLifecycleOwner) {
        //    textView.text = it
        //}
        init(homeViewModel, binding)
        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    private fun init(viewModel: HomeViewModel, binding: FragmentHomeBinding) {

        val vp2Adapter = ViewPage2Adapter(requireContext()).apply {
            viewModel.mDataSet.observe(viewLifecycleOwner) {
                dataSet = it
            }
        }

        binding.viewPager2.apply {
            adapter = vp2Adapter
            setPageTransformer(DIYPageTransformer())
        }
    }

}